//
//  ApeViewController+Pay.m
//  BDD
//
//  Created by Eton on 2017/4/8.
//  Copyright © 2017年 APE. All rights reserved.
//

#import "ApeViewController+Pay.h"
#import "AppRequest+Pay.h"

@implementation ApeViewController (Pay)

- (RACSignal *)paymentWithPayTask:(PayPlatformType) payTask
                  dataType:(PayDataType ) dataType
                money_real:(double ) money_real{
    return [self paymentWithPayTask:payTask
                    dataType:dataType
                  money_real:money_real
                    targetID:nil];
}

- (RACSignal *)paymentWithPayTask:(PayPlatformType) payTask
                  dataType:(PayDataType ) dataType
                money_real:(double ) money_real
                  targetID:(NSString *)targetID{
    
    kWeakSelf;
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[AppRequest requestCreatePayOrder:[RACTuple tupleWithObjects:@(dataType),@(money_real),targetID, nil]] subscribeNext:^(ApeModel * x1) {
            if (payTask == PayPlatformType_Balance) {
                [[AppRequest requestPaymentBalance:[RACTuple tupleWithObjects:x1.ID,@(dataType), nil]] subscribeNext:^(ApeModel * x2) {
                    [subscriber sendNext:@(PayResultType_Success)];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                } completed:^{
                    [weakSelf disLoading];
                }];
            }else{
                [[AppRequest requestPayment:[RACTuple tupleWithObjects:x1.ID,@(money_real),targetID, nil]] subscribeNext:^(PayParameterInfo * x2) {
                    
                    [[PayManager shareInstance] payWithPayPlatform:payTask parameterInfo:x2 callBackBlock:^(PayResultType resultType, NSString *errorStr) {
                        if (resultType == PayResultType_Success) {
                            [subscriber sendNext:@(PayResultType_Success)];
                        }else{
                            [subscriber sendError:[ApeResponseError errorWithDomain:@"" code:resultType userInfo:@{@"meesage":errorStr}]];
                        }
                    }];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                } completed:^{
                    [weakSelf disLoading];
                }];
            }
        } error:^(NSError *error) {
            [subscriber sendError:error];
        } completed:^{
            [weakSelf disLoading];
        }];
        return nil;
    }];
}

- (RACSignal *)paymentWithID:(NSString *) dataID
             dataType:(PayDataType ) dataType
              payTask:(PayPlatformType) payTask{
    kWeakSelf;
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[AppRequest requestPayment:[RACTuple tupleWithObjects:dataID,@(dataType),@(payTask), nil]] subscribeNext:^(PayParameterInfo * x) {
            [[PayManager shareInstance] payWithPayPlatform:payTask parameterInfo:x callBackBlock:^(PayResultType resultType, NSString *errorStr) {
                if (resultType == PayResultType_Success) {
                    [subscriber sendNext:@(PayResultType_Success)];
                }else{
                    [subscriber sendError:[ApeResponseError errorWithDomain:@"" code:resultType userInfo:@{@"meesage":errorStr}]];
                }
            }];
        } error:^(NSError *error) {
            [subscriber sendError:error];
        } completed:^{
            [weakSelf disLoading];
        }];
        return nil;
    }];
}

@end
